<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>登录接口测试</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 600px;
            margin: 50px auto;
            padding: 20px;
            background-color: #f5f5f5;
        }
        .container {
            background: white;
            padding: 30px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        .form-group {
            margin-bottom: 15px;
        }
        label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
        }
        input {
            width: 100%;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 4px;
            box-sizing: border-box;
        }
        button {
            background-color: #007bff;
            color: white;
            padding: 10px 20px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            margin-right: 10px;
        }
        button:hover {
            background-color: #0056b3;
        }
        .result {
            margin-top: 20px;
            padding: 15px;
            border-radius: 4px;
            white-space: pre-wrap;
            font-family: monospace;
        }
        .success {
            background-color: #d4edda;
            border: 1px solid #c3e6cb;
            color: #155724;
        }
        .error {
            background-color: #f8d7da;
            border: 1px solid #f5c6cb;
            color: #721c24;
        }
        .info {
            background-color: #d1ecf1;
            border: 1px solid #bee5eb;
            color: #0c5460;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>前后端登录接口测试</h1>
        
        <div class="form-group">
            <label for="username">用户名:</label>
            <input type="text" id="username" value="admin" placeholder="请输入用户名">
        </div>
        
        <div class="form-group">
            <label for="password">密码:</label>
            <input type="password" id="password" value="admin123" placeholder="请输入密码">
        </div>
        
        <button onclick="testLogin()">测试登录</button>
        <button onclick="checkAuthStatus()">检查认证状态</button>
        <button onclick="testLogout()">测试登出</button>
        <button onclick="clearResults()">清空结果</button>
        
        <div id="result" class="result info" style="display: none;"></div>
    </div>

    <script>
        const API_BASE_URL = 'http://localhost:8080/api/v1';
        
        function showResult(message, type = 'info') {
            const resultDiv = document.getElementById('result');
            resultDiv.className = `result ${type}`;
            resultDiv.textContent = message;
            resultDiv.style.display = 'block';
        }
        
        function clearResults() {
            document.getElementById('result').style.display = 'none';
        }
        
        async function testLogin() {
            const username = document.getElementById('username').value;
            const password = document.getElementById('password').value;
            
            if (!username || !password) {
                showResult('请输入用户名和密码', 'error');
                return;
            }
            
            try {
                showResult('正在发送登录请求...', 'info');
                
                const response = await fetch(`${API_BASE_URL}/auth/login`, {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify({
                        username: username,
                        password: password
                    })
                });
                
                const responseText = await response.text();
                let responseData;
                
                try {
                    responseData = JSON.parse(responseText);
                } catch (e) {
                    responseData = responseText;
                }
                
                const result = {
                    status: response.status,
                    statusText: response.statusText,
                    headers: Object.fromEntries(response.headers.entries()),
                    data: responseData
                };
                
                if (response.ok) {
                    // 保存token到localStorage
                    if (responseData && responseData.accessToken) {
                        localStorage.setItem('auth-token', responseData.accessToken);
                        localStorage.setItem('auth-user', JSON.stringify({
                            id: responseData.id,
                            username: responseData.username,
                            name: responseData.displayName,
                            email: responseData.email,
                            organizationId: responseData.orgId,
                            status: responseData.status?.toString() || '1'
                        }));
                    }
                    
                    showResult(`登录成功！\n\n响应详情：\n${JSON.stringify(result, null, 2)}`, 'success');
                } else {
                    showResult(`登录失败！\n\n响应详情：\n${JSON.stringify(result, null, 2)}`, 'error');
                }
                
            } catch (error) {
                showResult(`请求失败：${error.message}\n\n可能的原因：\n1. 后端服务未启动\n2. 网络连接问题\n3. CORS配置问题`, 'error');
            }
        }
        
        function checkAuthStatus() {
            const token = localStorage.getItem('auth-token');
            const user = localStorage.getItem('auth-user');
            
            if (token && user) {
                try {
                    const userData = JSON.parse(user);
                    showResult(`认证状态：已登录\n\nToken: ${token.substring(0, 50)}...\n\n用户信息：\n${JSON.stringify(userData, null, 2)}`, 'success');
                } catch (e) {
                    showResult(`认证状态：Token存在但用户信息解析失败\n\nToken: ${token}\nUser: ${user}`, 'error');
                }
            } else {
                showResult('认证状态：未登录\n\n本地存储中没有找到认证信息', 'info');
            }
        }
        
        function testLogout() {
            localStorage.removeItem('auth-token');
            localStorage.removeItem('auth-user');
            showResult('已清除本地认证信息', 'success');
        }
        
        // 页面加载时检查认证状态
        window.onload = function() {
            checkAuthStatus();
        };
    </script>
</body>
</html>